/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Vue;

import Modele.Bibliotheque;
import Modele.Repertoire;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Iterator;
import java.util.Vector;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;


/**
 *
 * @author Soufiane GARCH
 */
public class VueRepertoire extends JPanel implements ActionListener{

    private VuePrincipale vuePrincipale;
    private Bibliotheque biblio;
    private Vector repertoires;
    private Repertoire repertoireActuel;
    private DefaultListModel dlm;
    private JList list;
    private JLabel label;
    
    
    
    public VueRepertoire(Bibliotheque biblio, VuePrincipale p){
        this.vuePrincipale = p;
        label = new JLabel("Répertoire");
        this.biblio = biblio;
        this.dlm = new DefaultListModel();
        this.repertoires = biblio.getRepertoire();
        this.repertoireActuel = null;
        
        creerList();
        this.add(list);
        setPropertiesForList();
        setPropoerties();
        
                list.addMouseListener(new MouseListener() 
		{
			public void mouseClicked(MouseEvent e)
			{
				repertoireActuel=(Repertoire) list.getSelectedValue();
                                vuePrincipale.getBiblio().setAlbumActuel(null);
				vuePrincipale.afficheMiniature(repertoireActuel);
                                vuePrincipale.setImageActuel(null);
			}
			public void mouseEntered(MouseEvent e) {}
			public void mouseExited(MouseEvent e) {}
			public void mousePressed(MouseEvent e) {}
			public void mouseReleased(MouseEvent e) {}
		});
		
		
		addMouseListener(new MouseListener() 
		{
			public void mouseClicked(MouseEvent e)
			{
				if(repertoireActuel!=null)
				{
					repertoireActuel=(Repertoire) list.getSelectedValue();
                                        vuePrincipale.getBiblio().setAlbumActuel(null);
                                        vuePrincipale.setImageActuel(null);
                                        vuePrincipale.afficheMiniature(repertoireActuel);
				}
				
			}
			public void mouseEntered(MouseEvent e) {}
			public void mouseExited(MouseEvent e) {}
			public void mousePressed(MouseEvent e) {}
			public void mouseReleased(MouseEvent e) {}
		});
    }
    
    public void ajouterRepertoire(Repertoire rep){
        dlm.addElement(rep);
    }
    
    public void deleteRepertoire(Repertoire rep){
        if (dlm.contains(rep)){
            dlm.removeElement(rep);
        }
    }
    
    private void setPropertiesForList(){
        list.setVisibleRowCount(10);
        list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        
    }
    

   
    private void setPropoerties(){
        setLayout(new BoxLayout(this,BoxLayout.PAGE_AXIS));
		
        JPanel lab = new JPanel();
	lab.add(label);
	this.add(lab);
		
		
	JScrollPane scrollPane= new JScrollPane(list);
	scrollPane.setPreferredSize(new Dimension(280, 300));
	this.add(scrollPane);
        /*
        setPopupMenu();
        this.add(menu);
        this.setVisible(true);
        
        java.awt.event.MouseAdapter showMenu = new java.awt.event.MouseAdapter() {
            @Override
            public void mouseReleased(java.awt.event.MouseEvent e) {
                if(SwingUtilities.isRightMouseButton(e))
                menu.show(list, e.getX(), e.getY());
                repertoireActuel = (Repertoire) list.getSelectedValue();
            }
        };
        list.addMouseListener(showMenu);*/
    }
    /*
    private void setPopupMenu(){
        Ajouter = new JMenuItem("Ajouter");
        Ajouter.addActionListener(this);
        
        Renommer = new JMenuItem("Renommer");
        Renommer.addActionListener(this);
        
        Supprimer = new JMenuItem("Supprimer");
        Supprimer.addActionListener(this);
        //Ajouter.addActionListener(new ControleurAjouterRepertoire(pikasial));
        //Supprimer.addActionListener(new ControleurSupprimerRepertoire(pikasial));
        
        menu = new JPopupMenu();
        menu.add(Ajouter);
        menu.add(Renommer);
        menu.add(Supprimer);
    }*/
   
    private void creerList(){
        this.list = new JList(dlm);
        if (repertoires != null){
            Iterator it = repertoires.iterator();
            while(it.hasNext()){
                ajouterRepertoire( (Repertoire) it.next());
            }
        }
    }
            
    public Repertoire getAlbumActuel(){
        return this.repertoireActuel;
    }
    
    public Repertoire getRepertoireActuel(){
        return this.repertoireActuel;
    }
    
    public void update(){
        Iterator it = repertoires.iterator();
        while(it.hasNext()){
            Repertoire tmpRepertoire = (Repertoire) it.next();
            
            if (!(dlm.contains(tmpRepertoire))){
                ajouterRepertoire(tmpRepertoire);
            }
        }
        
        for(int i = 0; i < dlm.getSize(); i++){
            Repertoire tmpRepertoire = (Repertoire) dlm.get(i);
            if (!repertoires.contains(tmpRepertoire)){
                deleteRepertoire(tmpRepertoire);
            }
        }
    }
    
    public void setRepertoireActuel(){
        repertoireActuel = (Repertoire) list.getSelectedValue();
    }
    
    public void setRepertoireActuel(Repertoire rep){
        repertoireActuel = rep;
    }
    
    public JList getList(){
        return list;
    }
        
    public JLabel getLabel(){
        return label;
    }
    
    public void actionPerformed(ActionEvent e) {
        
        Object source = e.getSource();
        

        
    }


}

